package org.anno.chunkdb;

import com.google.common.collect.Iterables;

/**
 * Limits the iteration of rows.
 */
public class LimitQueryPlan extends QueryPlan {

  private final QueryPlan plan;
  private final int size;

  public LimitQueryPlan(QueryPlan plan, int size) {
    super(plan.table, plan.sortedColumn);
    this.plan = plan;
    this.size = size;
  }

  @Override
  public void accept(final ResponseVisitor visitor) {
    plan.accept(new ResponseVisitor() {
      @Override
      public void visitRows(Iterable<Host<RowVisitor>> rows) {
        visitor.visitRows(Iterables.limit(rows, size));
      }

      @Override
      public void visitColumn(int index, String label) {
        visitor.visitColumn(index, label);
      }
    });
  }
}
